package com.utils;

import com.framework.view.UpdateLog;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 */
public class GsonUtils {
    public static Gson gson = new Gson();

    //处理数据类型
    public static <T> T fromJson (String json, Type type) {
        Gson gson = new GsonBuilder().
                registerTypeAdapter(Double.class, new JsonSerializer<Double>() {
                    @Override
                    public JsonElement serialize (Double src, Type typeOfSrc, JsonSerializationContext context) {
                        if (src == src.longValue())
                            return new JsonPrimitive(src.longValue());
                        return new JsonPrimitive(src);
                    }
                }).create();
        return gson.fromJson(json, type);
    }

    public static String toJson (Object object) {
        return gson.toJson(object);
    }

    public static UpdateLog getUpdateLog (Long userId, Date updateTime) {
//        List<UpdateLog> logList=new ArrayList<UpdateLog>();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        UpdateLog updateLog = new UpdateLog(userId, format.format(updateTime));
//        logList.add(updateLog);
        return updateLog;
    }

    public static String addUpdateLog (String logList, Long userId, Date updateTime) {
        Type type = new TypeToken<List<UpdateLog>>() {
        }.getType();
        List<UpdateLog> fromJsonList = new ArrayList<UpdateLog>();
        if (logList != null) {
            GsonUtils.fromJson(logList, type);
        }
        UpdateLog addLog = getUpdateLog(userId, updateTime);
        fromJsonList.add(addLog);
        return toJson(fromJsonList);
    }

    public static String newUpdateLogs (Long userId, Date updateTime) {
        List<UpdateLog> logList = new ArrayList<UpdateLog>();
        UpdateLog updateLog = getUpdateLog(userId, updateTime);
        logList.add(updateLog);
        return toJson(logList);
    }

    /**
     * 运行了解示例
     *
     * @param args
     */
    public static void main (String[] args) {
//        List<UpdateLog> list = new ArrayList<UpdateLog>();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//        UpdateLog log1 = new UpdateLog(new Long(1), format.format(new Date()));
//        UpdateLog log2 = new UpdateLog(new Long(2), format.format(new Date()));
//        UpdateLog log3 = new UpdateLog(new Long(3), format.format(new Date()));
//
//        String str = GsonUtils.toJson(list);
//
//        System.out.println(str);
//        List<UpdateLog> fromJsonList = new ArrayList<UpdateLog>();
//        Type type = new TypeToken<List<UpdateLog>>() {
//        }.getType();
//        fromJsonList = GsonUtils.fromJson(str, type);
////        fromJsonList = gson.fromJson(str,listType);
//        System.out.println(fromJsonList.size());
//        for (UpdateLog log : fromJsonList) {
//            System.out.println(log.getId() + " " + log.getUpdateTime());
//        }
//
//        Map<String,Object> map = new HashMap<String,Object>();
//
//        map.put("name","name");
//        map.put("value",123);
        List<String> a = new ArrayList<>();
//        a.add("LX 3315");
//        a.add("LX_3436");
        String jsonStr = gson.toJson(a);
        System.out.println("ss:"+jsonStr.replace("[","").replace("]",""));
    }
}
